﻿using CodeGenerator.Common;
using CodeGenerator.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace CodeGenerator
{
    public partial class frmDBSqlServer : Form
    {
        public frmDBSqlServer()
        {
            InitializeComponent();
        }

        private void btnconnection_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.cbbServer.Text))
            {
                Utils.MessageBoxShow("服务器不能为空!");
            }
            else if ((this.cbbShenFenRZ.SelectedIndex == 1) && string.IsNullOrEmpty(this.txtUserName.Text))
            {
                Utils.MessageBoxShow("登陆名不能为空!");
            }
            else
            {
                try
                {
                    IDbObject obj2 = new DbMSSQLObject(this.cbbShenFenRZ.SelectedIndex == 0, this.cbbServer.Text, this.txtUserName.Text, this.txtPassword.Text);
                    DataTable dBList = obj2.GetDBList();
                    this.cbbDatabase.Items.Clear();
                    this.cbbDatabase.Items.Add("全部");
                    foreach (DataRow row in dBList.Rows)
                    {
                        this.cbbDatabase.Items.Add(row[0].ToString());
                    }
                    this.cbbDatabase.Enabled = true;
                    this.cbbDatabase.SelectedIndex = 0;
                    Utils.MessageBoxShow("连接成功!");
                }
                catch (Exception exception)
                {
                    Utils.MessageBoxShow("连接失败!\n\r" + exception.Message);
                    this.cbbDatabase.Enabled = false;
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.cbbServer.Text))
            {
                Utils.MessageBoxShow("服务器不能为空!");
            }
            else if ((this.cbbShenFenRZ.SelectedIndex == 1) && string.IsNullOrEmpty(this.txtUserName.Text))
            {
                Utils.MessageBoxShow("登陆名不能为空!");
            }
            else
            {
                string connectionString = this.createConnectionString((this.cbbDatabase.SelectedIndex == 0) ? "master" : this.cbbDatabase.Text);
                try
                {
                    using (SqlConnection connection2 = new SqlConnection(connectionString))
                    {
                        connection2.Open();
                    }
                }
                catch (Exception exception)
                {
                    Utils.MessageBoxShow("连接失败!\n\r" + exception.Message);
                    return;
                }
                Connection conection = new Connection
                {
                    Database = (this.cbbDatabase.SelectedIndex == 0) ? "all" : this.cbbDatabase.Text,
                    ID = Guid.NewGuid()
                };
                string[] textArray1 = new string[] { this.cbbServer.Text, "(MSSQL)[", conection.Database, "]" };
                conection.Name = string.Concat(textArray1);
                conection.DbType = DatabaseType.SqlServer.ToString();
                conection.ConnectionString = connectionString;
                Utils.AddConnection(conection);
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private string createConnectionString(string database)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Data Source=");
            builder.Append(this.cbbServer.Text);
            builder.Append(";Initial Catalog=");
            builder.Append(database);
            builder.Append(";");
            if (this.cbbShenFenRZ.SelectedIndex == 0)
            {
                builder.Append("Integrated Security=True");
            }
            else
            {
                builder.Append("User Id=");
                builder.Append(this.txtUserName.Text);
                builder.Append(";Password=");
                builder.Append(this.txtPassword.Text);
            }
            return builder.ToString();
        }

        private void frmDBSqlServer_Load(object sender, EventArgs e)
        {
          
            string[] objArray2 = new string[] { "Windows 身份认证", "SQL Server 身份认证" };
            this.cbbShenFenRZ.Items.AddRange(objArray2);

            this.cbbDatabase.Items.AddRange(new string[] { "全部" });
            this.cbbShenFenRZ.SelectedIndex = 0;
            this.cbbDatabase.SelectedIndex = 0;
        }

        private void cbbShenFenRZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbbShenFenRZ.SelectedIndex == 0)
            {
                this.txtPassword.Enabled = false;
                this.txtUserName.Enabled = false;
            }
            else
            {
                this.txtUserName.Enabled = true;
                this.txtPassword.Enabled = true;
            }
        }
    }
}
